import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { Location } from '@angular/common';
// 服务
import { LoadingAnimateService } from '../../../common/loading';
import { EventListenService } from './eventlisten.service';

import { EventListen } from './eventlisten';

import { AlertService } from '../../../common/alert';
@Component({
    moduleId: module.id,
    templateUrl: 'eventlisten.component.html',
    providers: [EventListenService]
})
export class EventlistenComponent implements OnInit {

    etype: string;
    action: string;
    previd: string;
    count: number;
    isNull: number;
    total: number;
    upid: string;
    pageObj = {};
    datas: EventListen[];
    page: number = 1;
    sumpage: number = 0;
    isNextpage: boolean;
    isPrevpage: boolean;
    constructor(
        private router: Router,
        private route: ActivatedRoute,
        private loadingService: LoadingAnimateService,
        private eventListenService: EventListenService,
        private alert: AlertService,
        private location: Location
    ) { }

    ngOnInit(): void {

        this.route.queryParams.subscribe(params => {
            this.etype = params['etype'] ? decodeURI(params['etype']) : '';
            this.action = params['action'] ? decodeURI(params['action']) : '';
            this.previd = params['previd'] ? decodeURI(params['previd']) : '';
            this.count = params['count'] ? params['count'] : 10;
            this.upid = params['upid'] ? decodeURI(params['upid']) : '';

            this.find();
        });
    }

    find(): void {
        this.loadingService.setValue(true);
        this.eventListenService.find(this.count, this.etype, this.action, this.previd).then(
            res => {
                this.loadingService.setValue(false);
                if (res == null || res.list == null || res.list.length == 0) {
                    this.isNull = 1;
                } else {
                    this.isNull = 0;
                }
                this.total = res.total;
                this.datas = res.list;
                this.pageShow();
            }
        );
    }
    del(item: EventListen): void {
        let msg='<div class="row">确定删除该条数据？</div>'+
        '<div class="row"><span class="col-4">类型:</span><span class="col-8">'+item.etype+'</span></div>'+ 
        '<div class="row"><span class="col-4">行为:</span><span class="col-8">'+item.action+'</span></div>'+ 
        '<div class="row"><span class="col-4">回调链接:</span><span class="col-8">'+item.hookurl+'</span></div>'
        this.alert.confirm(msg).subscribe(value=> {
            if (value) {
                this.loadingService.setValue(true);
                this.eventListenService.del(item.rid).then(
                res => {
                    this.loadingService.setValue(false);
                    if (res.ok) {
                        this.find()
                    } else {
                        this.alert.show("删除失败:" + res.msg);
                    }
                }
            );
            }
        });
    }
    edit(rid: string): void {
        let info: EventListen = null;
        for (let i = 0; i < this.datas.length; i++) {
            if (this.datas[i].rid == rid) {
                info = this.datas[i];
                break;
            }
        }
        const navigationExtras: NavigationExtras = {
            queryParams: info
        };
        this.router.navigate(['/client/eventlisten/edit'], navigationExtras);
    }
    // 搜索
    search(): void {
        let ps = {}
        if (this.etype != "") {
            ps["etype"] = encodeURI(this.etype);
        }
        if (this.action != "") {
            ps["action"] = encodeURI(this.action);
        }
        this.previd = "";
        this.pageObj = {};
        this.page=1;
        this.total=0;
        ps["count"] = this.count;
        ps["r"]=Math.random();
        const navigationExtras: NavigationExtras = {
            queryParams: ps
        };

        this.router.navigate(['/client/eventlisten'], navigationExtras);
        this.find();
    }
    pageShow(): void {
        if (!this.total) {
            this.isNextpage = false;
            this.isPrevpage = false;
            return
        }

        if (this.total % this.count > 0) {
            this.sumpage = (this.total - this.total % this.count) / this.count + 1;

        } else {
            this.sumpage = this.total / this.count;
        }
        if (this.sumpage <= 1) {
            this.isNextpage = false;
            this.isPrevpage = false;
            return
        }
        if (this.page == 1) {
            this.isNextpage = true;
            this.isPrevpage = false;
            return
        }
        if (this.page >= this.sumpage) {
            this.isNextpage = false;
            this.isPrevpage = true;
            return
        }
        this.isNextpage = true;
        this.isPrevpage = true;
        return
    }
    // 下一页
    nextPage(): void {
        let ps = {}
        if (this.etype != "") {
            ps["etype"] = encodeURI(this.etype);
        }
        if (this.action != "") {
            ps["action"] = encodeURI(this.action);
        }
        this.pageObj["page" + this.page] = this.previd;
        this.previd = this.datas[this.datas.length - 1].rid;
        ps["count"] = this.count;

        this.page++;
        ps["previd"] = this.previd;
        const navigationExtras: NavigationExtras = {
            queryParams: ps
        };
        this.pageShow();
        this.router.navigate(['/client/eventlisten'], navigationExtras);
        if (!this.previd) {
            this.find();
        }

    }
    // 上页
    prevPage(): void {
        let ps = {}
        if (this.etype != "") {
            ps["etype"] = encodeURI(this.etype)
        }
        if (this.action != "") {
            ps["action"] = encodeURI(this.action)
        }
        this.page--;
        this.previd = this.pageObj["page" + this.page];
        ps["count"] = this.count;
        ps["previd"] = this.previd;
        const navigationExtras: NavigationExtras = {
            queryParams: ps
        };

        if (this.page <= 0) {
            this.page = 1
        }
        this.pageShow();
        this.router.navigate(['/client/eventlisten'], navigationExtras);

    }
    // // 删除
    // del(id: string): void {
    //     if (confirm('确定删除该条数据？')) {
    //         this.eventListenService.del(id).then(() => {
    //             this.find(this.name);
    //         });
    //     }
    // }

}
